/*
 * Create Author  : jinpeng.chen
 * Create Date    : 2018-10-17
 * Project        : designpattern-learn
 * File Name      : InvokerChain.java
 *
 * Copyright (c) 2010-2018 by 18Birdies, LLC.
 * All Rights Reserved.
 *
 */
package com.charles.designpattern.responsibilitychain;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:  <p>
 *
 * @author : jinpeng.chen <p>
 * @version 1.0 2018-10-17
 * @since designpattern-learn 1.0
 */
public class FilterChain<REQ,RESP> implements Filter<REQ,RESP> {

  private List<Filter<REQ,RESP>> invokers = new ArrayList<>();
  private int index = 0;

  public FilterChain<REQ,RESP> addFilter(Filter<REQ,RESP> invoker) {
    invokers.add(invoker);
    return this;
  }

  @Override
  public  void doFilter(final REQ req, final RESP resp, final FilterChain<REQ,RESP> chain) {
    if (invokers.size() == 0 || index == invokers.size()) {
      return;
    }
    Filter<REQ, RESP> filter = invokers.get(index);
    index++;
    filter.doFilter(req, resp, chain);
  }
}
